package com.gdpu.service.impl;

import com.gdpu.bo.UmsMemberDetail;
import com.gdpu.mapper.UmsMemberMapper;
import com.gdpu.model.UmsMember;
import com.gdpu.model.UmsMemberExample;
import com.gdpu.service.UmsMemberService;
import com.gdpu.utils.JwtTokenUtil;
import com.gdpu.vo.UmsMemberInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserCache;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.core.userdetails.cache.NullUserCache;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:weiming
 * @Date:2021/6/12
 */
@Service
public class UmsMemberServiceImpl implements UmsMemberService, UserDetailsService {

    @Autowired
    private UmsMemberMapper umsMemberMapper;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private PasswordEncoder passwordEncoder;

    PasswordEncoder passwordEncoder(){return  new BCryptPasswordEncoder(); }

    @Override
    public String login(String username, String password) {
        String token = null;
        //获取用户对象
        UserDetails userDetails = loadUserByUsername(username);
        //加密密码后比较
        String encode = passwordEncoder().encode(password);
        if (!passwordEncoder().matches(password, userDetails.getPassword())) {
            return token;
        }
        token = jwtTokenUtil.generateToken(userDetails);
        return token;
    }


    @Override
    public UmsMemberInfoVo getUserInfo(){
        UmsMember umsMember = umsMemberMapper.selectByPrimaryKey(getCurrentMember().getId());
        UmsMemberInfoVo umsMemberInfoVo = new UmsMemberInfoVo();
        BeanUtils.copyProperties(umsMember, umsMemberInfoVo);
        return  umsMemberInfoVo;
    }

    @Override
    public UmsMember getCurrentMember() {
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        UmsMemberDetail  memberDetail = (UmsMemberDetail) authentication.getPrincipal();
        return memberDetail.getUmsMember();
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UmsMemberExample example = new UmsMemberExample();
        example.createCriteria().andUserNameEqualTo(username);
        List<UmsMember> umsMembers = umsMemberMapper.selectByExample(example);
        if (umsMembers.isEmpty()){
            throw new UsernameNotFoundException("用户名不存在");
        }
        UmsMemberDetail umsMemberDetail = new UmsMemberDetail(umsMembers.get(0));
        return  umsMemberDetail;
    }


    @Override
    public int regist(String username, String password) {

        UmsMember umsMember = new UmsMember();
        String encodePwd = passwordEncoder.encode(password);
        umsMember.setPassword(encodePwd);
        umsMember.setUserName(username);
        umsMember.setAccount(new BigDecimal("0"));
        int count  = umsMemberMapper.insert(umsMember);
        return count;
    }
}
